/**
 * 
 */
package com.yerba.ricecooker.scenarios;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import org.junit.Test;

import com.yerba.ricecooker.chart.AnnotatedTimeLineChart;
import com.yerba.ricecooker.chart.AnnotatedTimeLineDataRow;
import com.yerba.ricecooker.chart.AnnotatedTimeLineDataSet;
import com.yerba.ricecooker.datasource.YahooLoader;
import com.yerba.ricecooker.indicator.generic.IndicatorData1;
import com.yerba.ricecooker.indicator.technical.EMA;
import com.yerba.ricecooker.quote.Quote;
import com.yerba.ricecooker.quote.Quotes;
import com.yerba.ricecooker.utility.BrowserLauncher;
import com.yerba.ricecooker.utility.CommonHelper;

/**
 * @author mxie
 * 
 */
public class SP500Trend
{
    private AnnotatedTimeLineChart makeChart()
    {
        YahooLoader loader = new YahooLoader();
        loader.load("^GSPC");
        Quotes quotes = loader.getQuotes();

        EMA ema99 = new EMA(quotes, 99);
        EMA ema155 = new EMA(quotes, 155);
        EMA ema200 = new EMA(quotes, 200);

        // Testing Close and TrueClose
        int numOfLines = 4;

        AnnotatedTimeLineDataSet dataset = new AnnotatedTimeLineDataSet(numOfLines);
        dataset.getLabels().add(quotes.getSymbol());
        dataset.getLabels().add(ema99.name());
        dataset.getLabels().add(ema155.name());
        dataset.getLabels().add(ema200.name());

        // Set start date
        int i = quotes.index("1990-01-02");

        // Adding data
        for (; i < quotes.size(); i++)
        {
            AnnotatedTimeLineDataRow row = new AnnotatedTimeLineDataRow(dataset.getNumberOfLines());

            Quote quote = quotes.get(i);
            row.setDate(quote.getDate());
            row.addDataToRow(quote.getTrueClose());

            IndicatorData1 ema99val = ema99.get(row.getDate());
            if (ema99val != null)
                row.addDataToRow(ema99val.getFirst());
            else
                row.addDataToRow(0);

            IndicatorData1 ema155val = ema155.get(row.getDate());
            if (ema155val != null)
                row.addDataToRow(ema155val.getFirst());
            else
                row.addDataToRow(0);

            IndicatorData1 ema200val = ema200.get(row.getDate());
            if (ema200val != null)
                row.addDataToRow(ema200val.getFirst());
            else
                row.addDataToRow(0);

            dataset.addDataRow(row);
        }

        // Create charting object
        AnnotatedTimeLineChart chart = new AnnotatedTimeLineChart(dataset);
        chart.createChartPage();

        // Write page to file
        try
        {
            String chartfilename = CommonHelper.getTempFileName();
            FileOutputStream of = new FileOutputStream(chartfilename);
            OutputStreamWriter osw = new OutputStreamWriter(of, "UTF-8");
            osw.write(chart.getChartHtmlPage());
            osw.close();
            of.close();
        }
        catch (Exception e)
        {
            System.out.println(e.getStackTrace());
        }

        return chart;
    }

    @Test
    public void showChart()
    {
        AnnotatedTimeLineChart chart = makeChart();
        String chartfilename = CommonHelper.getTempFileName();

        // Write page to file
        try
        {
            FileOutputStream of = new FileOutputStream(chartfilename);
            OutputStreamWriter osw = new OutputStreamWriter(of, "UTF-8");
            osw.write(chart.getChartHtmlPage());
            osw.close();
            of.close();
        }
        catch (Exception e)
        {
            System.out.println(e.getStackTrace());
        }

        // Bring to browser
        BrowserLauncher.openURL(chartfilename);
    }
}
